<?php
class SpaceController extends CustomControllerAction {
	
	protected $user = null;
	protected $flag = false;
	private $identity = null;
	
	public function preDispatch() {
		parent::preDispatch();
		$request = $this->getRequest();
		if (strtolower($request->getActionName()) == 'usernotfound') {
			return;
		}
		
		$username = trim($request->getUserParam('username'));
		if (strlen($username) == 0 || $username == 'default') {
			$this->_forward('default');
		}
		$this->user = new DatabaseObject_User($this->db);
		if (!$this->user->loadByUsername($username)) {
			$this->_forward('usernotfound');
			return;
		}
		
		$this->identity = Zend_Auth::getInstance()->getIdentity();
		if (isset($this->identity) && 
		      $this->identity->userid == $this->user->userid) {
			$this->flag = true;
		}
		
		$this->breadcrumbs->addStep($this->user->username.'的空间',
		                            $this->getCustomUrl(array('username' => $this->user->username,
		                                                      'action'   => 'index')
		                            ));
	}
	
    public function defaultAction() {
        //TODO 用户空间主页
    }
    
	public function indexAction() {
		//TODO 为各个用户生成空间主页
	} 
	
	public function usernotfoundAction() {
		$username = trim($this->getRequest()->getUserParam('username'));
		$this->breadcrumbs->addStep('用户不存在');
		$this->view->requestedUsername = $username;
	}
	
	public function articleAction() {
		
		$month = $this->getRequest()->getQuery('month');
		
		if($month !='' && preg_match('/^(\d{4})-(\d{2})$/', $month, $matches)) {
			$y = $matches[1];
			$m = max(1, min(12, $matches[2]));
		} else {
			$y = date('Y');
			$m = date('n');
		}
		$date = array('y' => $y,
		              'm' => $m);
		$from = mktime(0, 0, 0, $m + 1, 1, $y);
		$to = mktime(0, 0, 0, $m, 1, $y) - 1;
		
		$options = array('userid' => $this->user->getId(),
		                 'from'   => $from,
		                 'to'     => $to,
		                 'order'  => 'a.dateline desc'
		                 );    
		$recentArticles = DatabaseObject_Article::GetArticles($this->db, $options);
		
		$totalArticles = DatabaseObject_Article::GetArticleCount($this->db, 
              array('userid' => $this->user->getId()));
        
        $this->breadcrumbs->addStep('文章浏览', 
            $this->getCustomUrl(array('username' => $this->user->username,
                                      'action'   => 'article')
                                     ));
                                     
        $this->view->flag = $this->flag;
        $this->view->date = $date;
        $this->view->recentArticles = $recentArticles;
        $this->view->totalArticles = $totalArticles;
	}
	
	public function circleAction() {
		//TODO 圈子显示浏览
	}
	
	public function albumAction() {
		//TODO 相册显示浏览
	}
	
	public function messageAction() {
		//TODO 消息显示浏览
	}
	
	public function friendAction() {
		//TODO 好友显示浏览
	}
	
	public function attachAction() {
		//TODO 附件显示浏览
	}
	
	public function geustbookAction() {
		//TODO 留言板显示浏览
	}
	
	public function applicationAction() {
		//TODO 应用显示浏览
	}
}
?>